package cn.mymanagesys.servlet;

import cn.mymanagesys.entity.SysPost;
import cn.mymanagesys.service.SysPostService;
import cn.mymanagesys.service.impl.SysPostServiceImpl;
import cn.mymanagesys.vo.AjaxResult;
import com.alibaba.fastjson.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/sysPost")
public class SysPostServlet extends HttpServlet {
    private SysPostService sysPostService = new SysPostServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setCharacterEncoding("utf-8");

        List<SysPost> sysPostList = sysPostService.list();

        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setCount(sysPostList.size());
        ajaxResult.setData(sysPostList);

        String jsonString = JSONArray.toJSONString(ajaxResult);

        response.getWriter().print(jsonString);

        response.getWriter().flush();
        response.getWriter().close();
    }
}
